package common.queue;


public class ArrayDeque<E> {

    // Attribute
    protected E[] arrays;
    protected int head;
    protected int tail;
    protected int size ;
    protected int count ;

    public ArrayDeque() {
        // Hier muss etwas angepasst werden
        this.size = 16;
        this.count = 0;
        arrays = (E[]) new Object[size];
    }

    public ArrayDeque(int size) {
        // Hier muss etwas angepasst werden
        this.size = size;
        arrays = (E[]) new Object[size];
    }

    /**
     * Inserts the specified element at the end of this deque.
     *
     * @param element the element to add
     * @return true, if the insertion was successful
     */
    public boolean offer(E element) {
        if (count == size) {
            return false;
        }
        if (size == 0) {
            arrays[head] = element;
            tail = head;
        } else {
            arrays[tail+1] = element;
            tail = (tail + 1) / size;
        }
        count++;
        return true;
    }

    /**
     * Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
     *
     * @return the head of this deque, or null if this deque is empty
     */
    public E peekFirst() {
        if (count == 0) {
            return null;
        }
        return arrays[head];
    }

    /**
     * Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
     *
     * @return the tail of this deque, or null if this deque is empty
     */
    public E peekLast() {
        if (count == 0) {
            return null;
        }
        return arrays[tail];
    }

    /**
     * Retrieves and removes the head of the common.queue represented by this deque (in other words,
     * the first element of this deque), or returns null if this deque is empty.
     *
     * @return the head of the common.queue represented by this deque, or null if this deque is empty
     */
    public E poll() {
        if (count == 0) {
            return null;
        }
        E e = arrays[head];
        head = (head + 1) % size;
        count--;
        return e;
    }
    
    /**
     * Retrieves and removes the last element of this deque, or returns null if this deque is empty.
     * 
     * @return the tail of this deque, or null if this deque is empty
     */
    public E pollLast() {
        if (count == 0) {
            return null;
        }
        E e = arrays[tail];
        tail = ((tail - 1) + size) % size;
        count--;
        return e;
    }

    /**
     * Pushes an element onto the common.stack represented by this deque.
     * In other words, inserts the element at the front of this deque.
     *
     * @param element the element to push
     * @return true, if the insertion was successful
     */
    public boolean push(E element) {
        if (count == size) {
            return false;
        }
        if (count == 0) {
            arrays[head] = element;
            tail = head;
        } else {
            head = ((head - 1) + size) % size;
            arrays[head] = element;
        }
        count++;
        return true;
    }

    /**
     * Pops an element from the common.stack represented by this deque.
     * In other words, removes and returns the first element of this deque.
     *
     * @return the element at the front of this deque (which is the top of the common.stack represented by this deque)
     */
    public E pop() {
        return poll();
    }

    /**
     * Returns the number of elements in this deque.
     *
     * @return the number of elements in this deque
     */
    public int size() {
        return count;
    }

    /**
     * Returns the current capacity of the common.list.
     * 
     * @return the current capacity of the array in this common.list
     */
    
    int capacity() {
        return size;
    }
    
}
